//
//  PZRReadShowTableViewCell.m
//  Moment
//
//  Created by pang on 15/12/23.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "PZRReadShowTableViewCell.h"

@implementation PZRReadShowTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self create];
    }
    return self;
}
-(void)create
{
    //创建轮播图
    self.mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/3 )];
    self.mainScrollView.backgroundColor = [UIColor yellowColor];
    self.mainScrollView.contentSize = CGSizeMake(Screen_Width * 3, Screen_height/3 );
    self.mainScrollView.pagingEnabled = YES;
    NSString *url = @"http://api2.pianke.me/read/columns";
    NSString *para = @"auth=CJFm1FH259KJDCsF1Gs3ikDJxQMejNrOMom1lsDzRlcWP22NJPSh5a7m34&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6";
    self.allArray = [NSMutableArray array];
    [GetData postUrl:url body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *bigArr = [bigDic objectForKey:@"carousel"];
        //NSLog(@"!!!%@",bigArr);
        for (NSMutableDictionary *dic in bigArr) {
            Read * read = [[Read alloc]init];
            [read setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:read];
            //NSLog(@"%ld",self.allArray.count);
        }
        [self.mainScrollView reloadInputViews];
        for (NSInteger i = 0; i<self.allArray.count; i++)
        {
            UIImageView *showImageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * SCREEN_WIDTH,0, Screen_Width, SCREEN_HEIGHT/3 )];
            //showImageView.backgroundColor = [UIColor orangeColor];
            [self.mainScrollView addSubview:showImageView];
            Read *read = [[Read alloc]init];
            read = [self.allArray objectAtIndex:i];
            NSURL *url = [NSURL URLWithString:read.img];
            //NSLog(@"%@",url);
            [showImageView sd_setImageWithURL:url];
            //[self.mainScrollView reloadInputViews];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];
    [self.contentView addSubview:_mainScrollView];
    
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}

-(void)timerAction
{
    if (self.mainScrollView.contentOffset.x == [[UIScreen mainScreen]bounds].size.width*(self.allArray.count -1)) {
        [self.mainScrollView setContentOffset:CGPointMake(0, self.mainScrollView.contentOffset.y)];
    }
    else{[self.mainScrollView setContentOffset:CGPointMake(self.mainScrollView.contentOffset.x+[[UIScreen mainScreen]bounds].size.width, self.mainScrollView.contentOffset.y)];}
}
- (void)awakeFromNib
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
